package dmeo2;

import sun.reflect.generics.tree.Tree;

import java.util.LinkedList;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        B.left = D;
        B.right = E;
        E.right = H;
        A.right = C;
        C.left = F;
        C.right = G;
        return A;
    }

    public void preOrder(TreeNode root){
        if(root == null){
            return ;
        }
        System.out.print(root.val+"  ");
        preOrder(root.left);
        preOrder(root.right);
    }

    void inOrder(TreeNode root){
        if(root == null){
            return ;
        }
        inOrder(root.left);
        System.out.print(root.val+"  ");
        inOrder(root.right);

    }

    void postOrder(TreeNode root){
        if(root == null){
            return ;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+"  ");

    }

    public int size(TreeNode root){
        if(root == null){
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }
    // 获取叶子节点的个数
    int getLeafNodeCount(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount(root.right) + getLeafNodeCount(root.left);
    }
    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }

    // 获取二叉树的高度
    int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }
        int Hleft = getHeight(root.left);
        int Hringht = getHeight(root.right);
        return (Hringht > Hleft? Hringht : Hleft) + 1;
    }
    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, int val){
        if(root ==null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        TreeNode Left = find(root.left,val);
        if(Left != null){
            return Left;
        }
        TreeNode Right = find(root.right,val);
        if(Right != null){
            return Right;
        }
        return null;
    }
    //层序遍历
   public void levelOrder(TreeNode root){
       Queue<TreeNode > queue = new LinkedList<>();
       if(root != null) {
           queue.offer(root);
       }
           while(!queue.isEmpty()){
               TreeNode top = queue.poll();
               System.out.print(top.val+ "");
               if(top.left != null){
                   queue.offer(top.left);
               }
               if(top.right != null){
                   queue.offer(top.right);
               }
           }
    }
    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>( );
        if(root != null){
            queue.offer(root);
        }
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur != null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur != null){
                return false;
            }
        }
        return true;


    }

}
